package xyz.yizhifeimao.controller.backend;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.yizhifeimao.common.Const;
import xyz.yizhifeimao.common.ResponseCode;
import xyz.yizhifeimao.common.ServiceResponse;
import xyz.yizhifeimao.pojo.Category;
import xyz.yizhifeimao.pojo.User;
import xyz.yizhifeimao.service.ICategoryService;
import xyz.yizhifeimao.service.IUserService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/manage/category")
public class CategoryManagerController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategoryService iCategoryService;

    @RequestMapping(value = "add_category.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId", defaultValue = "0") int parentId) {
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "您未登录，请登录");
        }
        // 校验是否是管理员
        if (iUserService.checkAdminRole(user).isSuccess()) {
            // 添加品类
            return iCategoryService.addCategory(categoryName, parentId);
        } else {
            return ServiceResponse.createByErrorMessage("无权限操作该交易，需管理员权限。");
        }
    }

    @RequestMapping(value = "set_category_name.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> setCategoryName(HttpSession session, Integer categoryId, String categoryName) {
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "您未登录，请登录");
        }
        // 校验是否是管理员
        if (iUserService.checkAdminRole(user).isSuccess()) {
            // 更新品类名称
            return iCategoryService.updateCategoryName(categoryName, categoryId);
        } else {
            return ServiceResponse.createByErrorMessage("无权限操作该交易，需管理员权限。");
        }
    }

    @RequestMapping(value = "get_category.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<List<Category>> getChildrenParallelCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "您未登录，请登录");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            // 更新品类名称
            return iCategoryService.getChildrenParallelCategory(categoryId);
        } else {
            return ServiceResponse.createByErrorMessage("无权限操作该交易，需管理员权限。");
        }
    }

    @RequestMapping(value = "get_deep_category.do", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse getCategoryAndDeepChildrenCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServiceResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "您未登录，请登录");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            // 更新品类名称
            return iCategoryService.getCategoryAndChildrenCategoryById(categoryId);
        } else {
            return ServiceResponse.createByErrorMessage("无权限操作该交易，需管理员权限。");
        }
    }



}
